//
//  CYVideoCollectionCell.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CYVideoCollectionCell.h"
#import "FitHeader.h"
#import "CYVideoTableViewCell.h"
#import <MJRefresh.h>
#import "HTTPMethod.h"
#import "changeString.h"
#import <UIImageView+WebCache.h>
@implementation CYVideoCollectionCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatViews];
        [self getData];
    }
    return self;
}

-(void)getData
{
    _indexArray = [NSMutableArray array];
    NSString *PostString = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=6793cf3e394e7e5a6f8f2d0716484ece&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C7C4F45AC-D834-40CC-8E57-1C73070C0250%7C28F136CD-C3C2-4542-AF9B-2BAD4F1067E6&format=json&loguid=&method=Info.getVideoIndexData&nonce=1446464484&sessionid=1446464474&signmethod=md5&timestamp=1446464484&uuid=8aa885128b339761cd4b5fc380d868d6&v=2&vc=43&vn=v5.4.0";
    NSString *bodyString = @"sign=&uid=&uuid=8aa885128b339761cd4b5fc380d868d6";
    [HTTPMethod getDataByString:PostString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        
        NSDictionary *dic = [data objectForKey:@"result"];
        _indexArray = [dic objectForKey:@"index_recommend"];
        [_tableView reloadData];
    }];
}

-(void)creatViews
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 49 * ht) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self addSubview:_tableView];
}

//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _indexArray.count;
}
//分区title
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_indexArray[section] valueForKey:@"CateName"];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * ht;
}
#pragma mark HeaderView样式
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hView = [[UIView alloc]init];
    hView.userInteractionEnabled = YES;
    hView.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * wt, 0, 150 * wt, 30 * ht)];
    titleLabel.text = [_indexArray[section] valueForKey:@"CateName"];
    [hView addSubview:titleLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(265 * wt, 0, 100 * wt, 30 * ht);
    button.tag = section + 1000;
    [button setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    [button setTitle:@"查看全部 >>" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showAll:) forControlEvents:UIControlEventTouchUpInside];
    [hView addSubview:button];
    
    return hView;
}

#pragma mark 点击查看全部得到 ID
-(void)showAll:(UIButton *)tagNum;
{
    NSString *idString = [_indexArray[tagNum.tag - 1000] valueForKey:@"CateId"];
    [_showDelegate ShowAllIdString:idString];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_indexArray[section] valueForKey:@"list"] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CYVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CYVideoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.titleLabel.text = [[[_indexArray[indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row ] objectForKey:@"Title"];
    cell.timeLabel.text = [[[_indexArray[indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row ] objectForKey:@"Collection"];
    cell.infoLabel.text = [[[_indexArray[indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row ] objectForKey:@"Stuff"];
    [cell.photoImage sd_setImageWithURL:[[[_indexArray[indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row ] objectForKey:@"Cover"]];
    return cell;
}
#pragma mark 点击Cell翻页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_indexArray.count != 0) {
        NSString *string = [[[_indexArray[indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row ] objectForKey:@"RecipeId"];
        [_delegate VideopassIdString:string];
    }
}

#pragma mark 设置section不在顶部停留
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 64 * ht;
        
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y >= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}



@end
